var text = '';
var resultText = '';

//发送函数
chrome.runtime.onMessage.addListener(function(request, sender, sendResponse)
{
	// console.log('收到来自content-script的消息：' ,request);
	// console.log(request, sender, sendResponse);
	// alert('我是后台，我已收到你的消息：' + JSON.stringify(request));
	if (request.type == 'receive') {
		if (request.text == text) {
			chrome.tabs.executeScript(null, {code:`javascript:
				var divbox = document.querySelectorAll('.xyPiWz6JGA');
				var timebox = document.querySelectorAll('._3fF9tZdS-P');
				divbox[divbox.length - 1].setAttribute('style','max-width:417px;flex-wrap:wrap;');
				divbox[divbox.length - 1].innerHTML += '<div class=\"_14RzJJb4VN\" style=\"width:100%;align-items:flex-start;\"><pre class=\"_2Zb8khbVxMFlHDDD2kMhKl\" style="padding:4px 8px;">${resultText}</pre></div>';
			`});
		} else {
			text = request.text;
			chrome.tabs.executeScript(null, {code:`javascript:
				var divbox = document.querySelectorAll('.xyPiWz6JGA');
				var timebox = document.querySelectorAll('._3fF9tZdS-P');
				divbox[divbox.length - 1].setAttribute('style','max-width:417px;flex-wrap:wrap;');
				divbox[divbox.length - 1].innerHTML += '<div class=\"_14RzJJb4VN\" style=\"width:100%;align-items:flex-start;\"><pre class=\"_2Zb8khbVxMFlHDDD2kMhKl\" style="padding:4px 8px;"></pre></div>';
			`});
			flutterMethod(JSON.stringify(request));
		}
	} else {
		flutterMethod(JSON.stringify(request));
	}


	return true;

});
function receivefn(val) {//改变页面
console.log(val)
    let value = JSON.parse(val)
	resultText = value.data;
    chrome.tabs.executeScript(null, {code:`javascript:
		var divbox = document.querySelectorAll('.xyPiWz6JGA');
		var texts = divbox[divbox.length - 1].innerText;
			if ('${value.text}' == texts) {
				divbox[divbox.length - 1].innerHTML = divbox[divbox.length - 1].innerHTML.substring(0,divbox[divbox.length - 1].innerHTML.length - 142);
				divbox[divbox.length - 1].setAttribute('style','max-width:417px;flex-wrap:wrap;');
				divbox[divbox.length - 1].innerHTML += '<div class=\"_14RzJJb4VN\" style=\"width:100%;align-items:flex-start;\"><pre class=\"_2Zb8khbVxMFlHDDD2kMhKl\" style="padding:4px 8px;">${value.data}</pre></div>';
			}
	`});
}

function sendfn(val) {
console.log("调用")
	chrome.tabs.executeScript(null, {code:`javascript:
		var textarea = document.querySelector('._1x2tTFFJ3K')
		textarea.value = '${val}';
		var nativeInputValueSetter = Object.getOwnPropertyDescriptor(window.HTMLTextAreaElement.prototype, "value").set;
		nativeInputValueSetter.call(textarea, '${val}');
		var ev2 = new Event('change', { bubbles: true});
		textarea.dispatchEvent(ev2);
		document.querySelector('._3pdzV0hrDx').click();
	`});
}
// document.querySelector('.src-components-ConversationDetailLayout-InputFieldLayout-ChatEditor-index__editor--3D_Zq').value = ${val};
// 		document.querySelector('.src-components-ConversationDetailLayout-InputFieldLayout-ChatEditor-index__button-active--xDGOG').click();


chrome.runtime.onInstalled.addListener( function() {
	chrome.declarativeContent.onPageChanged.removeRules(undefined,function(){
		chrome.declarativeContent.onPageChanged.addRules([
			{
				conditions:[
					new chrome.declarativeContent.PageStateMatcher({
						pageUrl:{urlContains:"shopee"}
					})
				],
				actions:[new chrome.declarativeContent.ShowPageAction()]
			}
		])
	});
});



